package Main;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import AI.AbstractAI;
import AI.BresenhamLineAIImproved;
import Actor.Actor;
import Map.Floor;
import Map.EnumTile;
import Map.MapLoader;
import Map.Tile;
import System.AbstractFrame;
import System.CreatorRunnable;
import System.Frame;
import System.Updater;
import View.MapView;
import View.ViewManager;
import Weapon.PathAI;
import Weapon.PathAIDetail;

@SuppressWarnings("serial")
public class Bootstrap extends JPanel {

	public static void main(String[] args) {


		Map<String, String> hm = new HashMap<String, String>();
		hm.put("h", "1000");
		hm.put("w", "1000");
		Floor map = MapLoader.getMap(hm);
//		for (int i = 0; i < 10; i++) {
//			for (int j = 0; j < 10; j++) {
//				System.out.println(map[i + 60][j + 10].getType());
//
//			}
//		}			
		Map<List<Point>, Integer> bullets = new ConcurrentHashMap<List<Point>, Integer>();
		Map<List<Point>, Integer> bullets1 = new ConcurrentHashMap<List<Point>, Integer>();
		//actor
		List<Actor> actor = new ArrayList<Actor>();
		actor.add(new Actor(50, 50));
		
		//monster
		List<Actor> monseter = new ArrayList<Actor>();
		int i = 0;
		while (i < 0) {
			Random randam = new Random();
			Actor ac = new Actor(randam.nextInt(50), randam.nextInt(50));
			//if (CollisionUtil.doCollisionCheck(ac, map)) {
				monseter.add(ac);
				i++;
			//}
		}
		

		//View
		ViewManager manager = new ViewManager(map, actor, monseter,bullets,bullets1);
		MapView mapView = new MapView(manager);
		
		//AI
		PathAIDetail pathAI1 = new PathAIDetail(bullets1);
		PathAI pathAI = new PathAI(bullets);
		AbstractAI ai = new BresenhamLineAIImproved(actor, monseter, map,bullets,manager);
		//listener
		KeyListener keyListener = new MyKeyListener(actor.get(0), map,manager);
		MouseMotionListener mouseMotionListener = new MyMouseMotionListener(manager);
		MyMouseActionListener mouseActionListener = new MyMouseActionListener(pathAI, actor.get(0),manager,pathAI1);

		//double buffer
		BlockingQueue<BufferStrategy> bufferStrategyQueue = new ArrayBlockingQueue<BufferStrategy>(1);
		
		
		AbstractFrame gameData = new Frame(mapView, ai);
		final Updater gameUpdater = new Updater(gameData, bufferStrategyQueue,
				Constant.framewWidht + 1, Constant.frameHeight + 1);

		Runnable guiCreator = new CreatorRunnable(Constant.framewWidht + 1, Constant.frameHeight + 1, gameData,
				gameUpdater, bufferStrategyQueue, keyListener,
				mouseMotionListener, mouseActionListener);

		try {
			SwingUtilities.invokeAndWait(guiCreator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				gameUpdater.updateLoop();
			}
		}).start();
	}

}
